package com.macro.mall.seckill.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.constant.RedisConstant;
import com.macro.mall.common.exception.ApiException;
import com.macro.mall.common.service.RedisService;
import com.macro.mall.model.PmsProduct;
import com.macro.mall.model.SmsFlashPromotion;
import com.macro.mall.seckill.domain.SecKillListDto;
import com.macro.mall.seckill.domain.SecKillProductDetailDto;
import com.macro.mall.seckill.service.SecKillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: mall
 * @ClassName: FlashPromotionController
 * @description: 秒杀活动控制器
 * @author: tan
 * @create: 2024-06-14 10:37
 **/
@RestController
@RequestMapping("/seckill")
public class FlashPromotionController {
    @Autowired
    private RedisService redisService;

    @Autowired
    private SecKillService secKillService;

    /**
     * 查询秒杀活动列表
     * @date 2024/6/25 15:10
     * @return java.util.List<com.macro.mall.model.SmsFlashPromotion>
     */
    @GetMapping("/findFlashPromotionList")
    public CommonResult<SecKillListDto> findFlashPromotionList(){
        List<SmsFlashPromotion> flashPromotions = (List<SmsFlashPromotion>)redisService.hValues(RedisConstant.REDIS_FLASH_PROMOTION);
        SmsFlashPromotion currentPromotion = secKillService.findCurrentPromotion(flashPromotions);
        if(currentPromotion==null){
            throw new ApiException("当前无秒杀活动");
        }
        //获取秒杀活动下的商品信息
        List<PmsProduct> products = (List<PmsProduct>)redisService.hValues(RedisConstant.REDIS_FLASH_PROMOTION_PRODUCT + currentPromotion.getId());
        SecKillListDto secKillListDto = new SecKillListDto();
        secKillListDto.setFlashPromotionList(flashPromotions);
        secKillListDto.setCurrentFlashPromotion(currentPromotion);
        secKillListDto.setProductList(products);
        return CommonResult.success(secKillListDto);
    }




    /**
     * 秒杀商品详情
     * @date 2024/6/14 10:45
     * @param promotionId 活动ID
     * @param productId 商品ID
     */
    @GetMapping("/findPromotionProductDetail")
    public CommonResult<SecKillProductDetailDto> findPromotionProductDetail(Long promotionId, Long productId){
        //获取活动信息
        SmsFlashPromotion flashPromotion = (SmsFlashPromotion)redisService.hGet(RedisConstant.REDIS_FLASH_PROMOTION, promotionId.toString());
        //获取商品信息
        PmsProduct product = (PmsProduct)redisService.hGet(RedisConstant.REDIS_FLASH_PROMOTION_PRODUCT + promotionId, productId.toString());
        SecKillProductDetailDto secKillProductDetailDto = new SecKillProductDetailDto();
        secKillProductDetailDto.setProduct(product);
        secKillProductDetailDto.setFlashPromotion(flashPromotion);
        return CommonResult.success(secKillProductDetailDto);
    }
}